﻿using CRMG.ResourceManagement.Resources;
using CRMG.UserManagement.Source.BO;
using CRMG.UserManagement.Source.DL;
using Guru.ErrorHandling;
using System;
using System.Collections.Generic;
using System.Data;

namespace CRMG.UserManagement
{
    public class UserManager
    {
        #region [ User Operations ]

        public DataTable GetUserList()
        {
            try
            {
                UserDL usrDL = new UserDL();
                return usrDL.GetUserList();
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1004),
                    ErrorCode = 1004
                };
                throw new GuruException(exc, logger);
            }
        }

        public User GetUser(string username, string pass)
        {
            try
            {
                UserDL userDL = new UserDL();
                return userDL.GetUser(username, pass);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1000),
                    ErrorCode = 1000
                };
                throw new GuruException(exc, logger);
            }
        }

        public User GetUser(int userId)
        {
            try
            {
                UserDL userDL = new UserDL();
                return userDL.GetUser(userId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1001),
                    ErrorCode = 1001
                };
                throw new GuruException(exc, logger);
            }
        }

        public int AddUser(User usr)
        {
            try
            {
                UserDL usrDL = new UserDL();
                return usrDL.Insert(usr);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1002),
                    ErrorCode = 1002
                };
                throw new GuruException(exc, logger);
            }
        }

        public int AddUserAndGetId(User usr)
        {
            try
            {
                UserDL usrDL = new UserDL();
                return usrDL.InsertAndGetId(usr);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1003),
                    ErrorCode = 1003
                };
                throw new GuruException(exc, logger);
            }
        }

        public DataTable GetTrainerUsers()
        {
            try
            {
                UserDL usrDL = new UserDL();
                return usrDL.GetTrainers();
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ Role Operations ]

        public int AddRole(Roles pRoles)
        {
            try
            {
                RolesDL rolesDL = new RolesDL();
                return rolesDL.InsertAndGetId(pRoles);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1004),
                    ErrorCode = 1004
                };
                throw new GuruException(exc, logger);
            }
        }

        public void UpdateRole(Roles pRoles)
        {
            try
            {
                RolesDL rolesDL = new RolesDL();
                rolesDL.Update(pRoles);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1005),
                    ErrorCode = 1005
                };
                throw new GuruException(exc, logger);
            }
        }

        public void DeleteRole(Roles pRoles)
        {
            try
            {
                RolesDL rolesDL = new RolesDL();
                rolesDL.Update(new Roles() { OBJID = pRoles.OBJID, IsActive = 0 });
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1006),
                    ErrorCode = 1006
                };
                throw new GuruException(exc, logger);
            }
        }

        public List<string> GetOperationCodesOfUser(int userId)
        {
            try
            {
                OperationsDL opsDL = new OperationsDL();
                return opsDL.GetOperationCodesOfUser(userId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1014),
                    ErrorCode = 1014
                };
                throw new GuruException(exc, logger);
            }
        }

        #endregion


        #region [ Operation operations ]

        public int AddOperation(Operations ops)
        {
            try
            {
                OperationsDL opsDL = new OperationsDL();
                return opsDL.InsertAndGetId(ops);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1007),
                    ErrorCode = 1007
                };
                throw new GuruException(exc, logger);
            }
        }

        public void UpdateOperation(Operations ops)
        {
            try
            {
                OperationsDL opsDL = new OperationsDL();
                opsDL.Update(ops);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1008),
                    ErrorCode = 1008
                };
                throw new GuruException(exc, logger);
            }
        }

        public void DeleteOperation(Operations ops)
        {
            try
            {
                OperationsDL opsDL = new OperationsDL();
                opsDL.Update(new Operations() { OBJID = ops.OBJID, IsActive = 0 });
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1009),
                    ErrorCode = 1009
                };
                throw new GuruException(exc, logger);
            }
        }

        #endregion


        #region [ UserRole Operations ]

        public void AddUserRole(int userId, List<int> pRoles)
        {
            try
            {
                UserRoleDL usroleDL = new UserRoleDL();
                UserRole usrole;
                foreach (int roleId in pRoles)
                {
                    usrole = new UserRole() { UserId = userId, RoleId = roleId };
                    usroleDL.Insert(usrole);
                }
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1010),
                    ErrorCode = 1010
                };
                throw new GuruException(exc, logger);
            }
        }

        public void DeleteRolesOfUser(int userId)
        {
            try
            {
                UserRoleDL usrroleDL = new UserRoleDL();
                usrroleDL.DeleteRolesOfUser(userId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1011),
                    ErrorCode = 1011
                };
                throw new GuruException(exc, logger);
            }
        }

        #endregion


        #region [ RoleOperation Operations ]

        public void AddRoleOperations(int roleId, List<int> opList)
        {
            try
            {
                RoleOperationDL roleopDL = new RoleOperationDL();
                RoleOperation roleOp;
                foreach (int opId in opList)
                {
                    roleOp = new RoleOperation() { RoleId = roleId, OperationId = opId };
                    roleopDL.Insert(roleOp);
                }
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1012),
                    ErrorCode = 1012
                };
                throw new GuruException(exc, logger);
            }
        }

        public void DeleteOperationsOfRole(int roleId)
        {
            try
            {
                RoleOperationDL roleopDL = new RoleOperationDL();
                roleopDL.DeleteOperationsOfRole(roleId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(1013),
                    ErrorCode = 1013
                };
                throw new GuruException(exc, logger);
            }
        }


        #endregion


    }
}
